package com.qst.survey.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qst.survey.entity.Replay;
import com.qst.survey.entity.User;
import com.qst.survey.mapper.ReplayMapper;
import com.qst.survey.service.ReplayService;
import com.qst.survey.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Date: 2021/3/27 11:36
 * @Description: 简答题回复具体类
 */


@Service
public class ReplayServiceImpl implements ReplayService {

    @Autowired
    ReplayMapper replayMapper;

    @Autowired
    UserService userService;

    /**
     * @param replay ：回复实体类
     * @description TODO   修改一个选择数量+1
     * @date 2021/4/2
     */
    @Override
    public void insertReplay(Replay replay) {
        replayMapper.insert(replay);
    }

    /**
     * @param ids   ：
     * @param texts ：
     * @description TODO   修改一组选择数量+1
     * @date 2021/4/2
     */
    @Override
    public void insertReplayList(ArrayList<Integer> ids, ArrayList<String> texts, Integer userId) {
        User oneById = userService.findOneById(userId);
        for (int i = 0; i < ids.size(); i++) {
            Replay replay = new Replay();
            replay.setTopicId(ids.get(i));
            replay.setAge(oneById.getAge());
            replay.setUserId(userId);
            replay.setReplayText(texts.get(i));
            insertReplay(replay);
        }
    }

    /**
     * @param topicId ：问题id
     * @return java.util.List
     * @description TODO   查询一个题目的所有回答
     * @date 2021/4/2
     */
    @Override
    public List findAllById(Integer topicId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("topic_id", topicId);
        List list = replayMapper.selectList(queryWrapper);
        return list;
    }

    /**
     * @param topicId 问题id
     * @return java.lang.Integer
     * @description TODO   统计一个题目的所有回答数量
     * @date 2021/4/2
     */
    @Override
    public Integer countById(Integer topicId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("topic_id", topicId);
        Integer integer = replayMapper.selectCount(queryWrapper);
        return integer;
    }

    /**
     * @param page    分页类
     * @param topicId 问题id
     * @return com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.qst.survey.entity.Replay>
     * @description TODO   通过题目id分页查出每个回答
     * @date 2021/4/2
     */
    @Override
    public Page<Replay> findPageById(Page page, Integer topicId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("topic_id", topicId);
        Page page1 = replayMapper.replayList(page, topicId);
        return page1;
    }
}
